﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace PlaylistGenerator_v2._0
{
    public class DigitalDisplay : UserControl
    {
        #region Variáveis e Constantes
        private IContainer componentes;
        
        private String text;
        private int scrollAmount = 5;
        internal Timer timerScroll;
        private int posicaoInicial = 0;


        [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                this.Invalidate();
            }
        }

        public int ScrollTimeInterval
        {
            get
            {
                return timerScroll.Interval;
            }
            set
            {
                timerScroll.Interval = value;
            }
        }

        [DefaultValue(10)]
        public int ScrollPixelAmount
        {
            get
            {
                return scrollAmount;
            }
            set
            {
                scrollAmount = value;
            }
        }
        #endregion

        #region Inicialização
        public DigitalDisplay()
        {
            InitializeComponent();
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (componentes != null)
                {
                    componentes.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.componentes = new Container();
            this.timerScroll = new Timer(this.componentes);
            // 
            // timerScroll
            // 
            this.timerScroll.Tick += new EventHandler(this.timerScroll_Tick);
            // 
            // DigitalDisplay
            // 
            this.Name = "DigitalDisplay";
            this.Size = new Size(360, 104);
            this.Load += new EventHandler(this.DigitalDisplay_Load);

        }
        #endregion

        #region Eventos
        private void DigitalDisplay_Load(object sender, System.EventArgs e)
        {
            this.ResizeRedraw = true;
            if (!this.DesignMode)
            {
                timerScroll.Enabled = true;
            }

        }

        private void timerScroll_Tick(object sender, System.EventArgs e)
        {
            //Esquerda para Direita
            //posicaoInicial += scrollAmount;

            //Direita para Esquerda
            posicaoInicial -= scrollAmount;

            // Force a refresh.
            this.Invalidate();

        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            // Do nothing.
            // To prevent flicker, we will draw both the background and the text
            // to a buffered image, and draw it to the control all at once.
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // The following line avoids a design-time error that would
            // otherwise occur when the control is first loaded (but does not yet
            // have a defined size).
            if (e.ClipRectangle.Width == 0)
            {
                return;
            }

            base.OnPaint(e);

            //Esquerda para Direita
            //if (position > this.Width)

            //Direita para Esquerda
            if (posicaoInicial < 0 - (int)e.Graphics.MeasureString(text, this.Font).Width)
            {
                // Reinicia o scroll do texto
                
                //Esquerda para Direita
                //position = -(int)e.Graphics.MeasureString(text, this.Font).Width;

                //Direita para Esquerda
                posicaoInicial = this.Width;
            }

            Bitmap blt = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);
            Graphics g = Graphics.FromImage(blt);

            g.FillRectangle(new SolidBrush(this.BackColor), e.ClipRectangle);
            g.DrawString(text, this.Font, new SolidBrush(this.ForeColor), posicaoInicial, 0);

            e.Graphics.DrawImageUnscaled(blt, 0, 0);

            g.Dispose();
        }
        #endregion

    }
}
